﻿Imports System.Net
Imports System.Xml

Public Class Main
	Public prevForm As Form

	Private Sub Main_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		If Not IsNothing(prevForm) Then
			prevForm.Show()
		End If
	End Sub

	Private Sub Main_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

	End Sub

	Private Sub OptionsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OptionsToolStripMenuItem.Click
		If DialogOptions.ShowDialog = DialogResult.OK Then

		End If

	End Sub

	Private Sub ButtonAlbumGetInfo_Click(sender As System.Object, e As System.EventArgs) Handles ButtonAlbumGetInfo.Click
		Dim methodAlbumGetInfo = New AlbumGetInfo(TextBoxMainArtist.Text, TextBoxMainAlbum.Text, TextBoxMainMBID.Text, TextBoxMainLanguage.Text, TextBoxMainAutocorrect.Text)
		methodAlbumGetInfo.sendRequest()
		TextBoxXML.Text = methodAlbumGetInfo.xmlresponse.ToString
		Dim response As Album = methodAlbumGetInfo.getResponse().album
		Try
			PictureBoxAlbumGetInfoImage.ImageLocation = response.image(2)

			Dim count As Integer = 1
			ListViewAlbumGetInfo.Items.Clear()
			For Each track As Track In response.tracks
				ListViewAlbumGetInfo.Items.Add(New ListViewItem(count.ToString, ListViewAlbumGetInfo.Groups("Tracks")))
				ListViewAlbumGetInfo.Items(count - 1).SubItems.Add(track.name)
				Dim num As Integer = Integer.Parse(track.duration)
				ListViewAlbumGetInfo.Items(count - 1).SubItems.Add((num \ 60).ToString + ":" + (num Mod 60).ToString("d2"))
				ListViewAlbumGetInfo.Items(count - 1).SubItems.Add(track.url)
				ListViewAlbumGetInfo.Items(count - 1).SubItems.Add(track.mbid)
				count = count + 1
			Next
			TextBoxAlbumGetInfoResults.Text = "Artist: " + response.artist + ", Album: " + response.name + ", Release Date: " + response.releaseDate + vbNewLine + "Play Count: " + response.playcount + ", Listeners: " + response.listeners
			'WebBrowser1.Navigate(response.url)
		Catch ex As Exception

		End Try
	End Sub

	Private Sub ButtonAuthGetToken_Click(sender As System.Object, e As System.EventArgs) Handles ButtonAuthGetToken.Click
		Dim methodAuth As New AuthGetToken
		Dim doc As XDocument = methodAuth.getToken()
		TextBoxXML.Text = doc.ToString
		My.Settings.token = methodAuth.ExtractToken(doc)
	End Sub

	Private Sub ButtonAuthGetSession_Click(sender As System.Object, e As System.EventArgs) Handles ButtonAuthGetSession.Click
		Dim methodAuth As New AuthGetSession
		Dim doc As XDocument = methodAuth.getSession()
		TextBoxXML.Text = doc.ToString
		Try
			My.Settings.sk = doc.<lfm>.<session>.<key>.Value
		Catch ex As Exception

		End Try
	End Sub

	Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
		Me.Close()
	End Sub

	Private Sub PrintToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles PrintToolStripMenuItem.Click
		If PrintDialog1.ShowDialog = DialogResult.OK Then

		End If
	End Sub

	Private Sub ButtonTrackGetInfo_Click(sender As System.Object, e As System.EventArgs) Handles ButtonTrackGetInfo.Click
		Dim methodTrackGetInfo = New TrackGetInfo(TextBoxMainArtist.Text, TextBoxMainTrack.Text, TextBoxMainMBID.Text, TextBoxMainAutocorrect.Text)
		methodTrackGetInfo.sendRequest()
		TextBoxXML.Text = methodTrackGetInfo.xmlresponse.ToString

	End Sub

	Private Sub ButtonMainTest_Click(sender As System.Object, e As System.EventArgs)
	End Sub

	Private Sub ButtonAlbumSearch_Click(sender As System.Object, e As System.EventArgs) Handles ButtonAlbumSearch.Click
		Dim methodAlbumSearch = New AlbumSearch(TextBoxMainAlbum.Text, TextBoxMainLimit.Text, TextBoxMainPage.Text)
		methodAlbumSearch.sendRequest()
		TextBoxXML.Text = methodAlbumSearch.xmlresponse.ToString

		Dim response = methodAlbumSearch.getResponse()

		ListViewAlbumSearch.Items.Clear()
		ListViewAlbumSearch.Columns.Clear()
		ListViewAlbumSearch.Groups.Clear()
		ListViewAlbumSearch.Columns.Add("name")
		ListViewAlbumSearch.Groups.Add("Albums", "Albums")

		Dim imageList As New ImageList()
		imageList.ImageSize = New Size(34, 34)
		Dim art As New PictureBox
		For Each album As Album In response.albums
			art.Load(album.image(0))
			imageList.Images.Add(art.Image)
			Dim item = New ListViewItem(album.name, imageList.Images.Count() - 1, ListViewAlbumSearch.Groups(0))

			item.SubItems.Add(album.artist)
			ListViewAlbumSearch.Items.Add(item)

		Next
		art.Dispose()

		ListViewAlbumSearch.SmallImageList = imageList
		ListViewAlbumSearch.LargeImageList = imageList

	End Sub

	Private Sub ButtonAlbumGetTags_Click(sender As System.Object, e As System.EventArgs) Handles ButtonAlbumGetTags.Click
		Dim methodAlbumGetTags = New AlbumGetTags(TextBoxMainArtist.Text, TextBoxMainAlbum.Text, TextBoxMainMBID.Text, TextBoxMainAutocorrect.Text, My.Settings.user_name)
		methodAlbumGetTags.sendRequest()
		TextBoxXML.Text = methodAlbumGetTags.xmlresponse.ToString
	End Sub

	Private Sub ListBoxAlbumSearch_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBoxAlbumSearch.SelectedIndexChanged
		Select Case ListBoxAlbumSearch.SelectedIndex
			Case 0
				ListViewAlbumSearch.View() = View.LargeIcon
			Case 1
				ListViewAlbumSearch.View() = View.Details
			Case 2
				ListViewAlbumSearch.View() = View.SmallIcon
			Case 3
				ListViewAlbumSearch.View() = View.List
			Case 4
				ListViewAlbumSearch.View() = View.Tile

		End Select
	End Sub

	Private Sub ButtonAlbumSearchApplication_Click(sender As System.Object, e As System.EventArgs) Handles ButtonAlbumSearchApplication.Click
		FormSearch.Show()
	End Sub

	Private Sub ButtonFormRadioSearch_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFormRadioSearch.Click
		FormRadioSearch.Show()
	End Sub

	Private Sub ButtonAuthGetAuthentication_Click(sender As System.Object, e As System.EventArgs) Handles ButtonAuthGetAuthentication.Click
		WebBrowser1.Navigate("http://www.last.fm/api/auth/?api_key=" + My.Settings.api_key + "&token=" + My.Settings.token)
		'Dim methodAuth As New AuthGetAuthentication
		'Dim doc As XDocument = methodAuth.sendRequest()
		'TextBoxXML.Text = doc.ToString
	End Sub
End Class
